package com.siderakis.gwt.cellwidgets.client.colorpicker;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.siderakis.gwt.cellwidgets.client.dialog.Popup;

public class ColorPickerDialog extends Popup {
	private SaturationLightnessPicker slPicker;
	private HuePicker huePicker;
	private String color;

	@Override
	protected void buttonClicked(final Widget button) {
		// remember color when "OK" is clicked
		if (button == getOkButton()) {
			color = slPicker.getColor();
		}

		close(button == getCancelButton());
	}

	@Override
	protected Widget createDialogArea() {
		final HorizontalPanel panel = new HorizontalPanel();

		// the pickers
		slPicker = new SaturationLightnessPicker();
		panel.add(slPicker);
		huePicker = new HuePicker();
		panel.add(huePicker);

		// bind saturation/lightness picker and hue picker together
		huePicker.addHueChangedHandler(new IHueChangedHandler() {
			@Override
			public void hueChanged(final HueChangedEvent event) {
				slPicker.setHue(event.getHue());
			}
		});

		return panel;
	}

	public String getColor() {
		return color;
	}

	public void setColor(final String color) {
		final int[] rgb = ColorUtils.getRGB(color);
		final int[] hsl = ColorUtils.rgb2hsl(rgb);
		huePicker.setHue(hsl[0]);
		slPicker.setColor(color);
	}
}
